package com.demo2.util;

import org.springframework.core.MethodParameter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter;

import java.lang.reflect.Type;
import java.util.Objects;

/**
 * Copyright
 *
 * @Author WuHuang
 * @Date 2022/5/31
 */
@ControllerAdvice(basePackages = "com.demo2.controller")
public class GlobalRequestBodyAdvice extends RequestBodyAdviceAdapter {


    @Override
    public boolean supports(MethodParameter methodParameter, Type type,
                            Class<? extends HttpMessageConverter<?>> aClass) {
        System.out.println("22222请求");

        if (Objects.nonNull(methodParameter.getParameterAnnotation(RequestBody.class))) {
            return true;
        }
        return false;
    }

    @Override
    public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        System.out.println("1111请求");
        return body;
    }
}
